<!DOCTYPE html>
<html lang="en">

    <head>

        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <meta name="description" content="">
        <meta name="author" content="">

        <title>Kura's Hardware Shop Offical Page</title>

        <!-- Bootstrap Core CSS -->
        <link href="css/bootstrap.min.css" rel="stylesheet">

        <!-- MetisMenu CSS -->
        <link href="css/plugins/metisMenu/metisMenu.min.css" rel="stylesheet">

        <!-- Timeline CSS -->
        <link href="css/plugins/timeline.css" rel="stylesheet">

        <!-- Custom CSS -->
        <link href="css/sb-admin-2.css" rel="stylesheet">

        <!-- Morris Charts CSS -->
        <link href="css/plugins/morris.css" rel="stylesheet">

        <!-- DataTables CSS -->
        <link href="css/plugins/dataTables.bootstrap.css" rel="stylesheet">

        <!-- Custom Fonts -->
        <link href="font-awesome-4.1.0/css/font-awesome.min.css" rel="stylesheet" type="text/css">

        <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
        <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
        <!--[if lt IE 9]>
            <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
            <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
        <![endif]-->

    </head>

    <body>
        <?php
        session_start();
        if (!isset($_SESSION['role'])) {
            header("Location: login.php");
        }
        include_once './script/dbconnect.php';
        include_once("./script/statisticScript.php");
        ?>
        <div id="wrapper">

            <!-- Navigation -->
            <nav class="navbar navbar-default navbar-static-top" role="navigation" style="margin-bottom: 0">
                <div class="navbar-header">
                    <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
                        <span class="sr-only">Toggle navigation</span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                    </button>
                    <a class="navbar-brand" href="index.html">Xin chào <?php echo $_SESSION['name'] ?></a> <!-- get User late -->
                </div>
                <!-- /.navbar-header -->

                <ul class="nav navbar-top-links navbar-right">

                    <li class="dropdown">
                        <a class="dropdown-toggle" data-toggle="dropdown" href="#">
                            <i class="fa fa-user fa-fw"></i>  <i class="fa fa-caret-down"></i>
                        </a>
                        <ul class="dropdown-menu dropdown-user">
                            <li><a href="#"><i class="fa fa-user fa-fw"></i> Thông tin cá nhân</a>
                            </li>

                            <li class="divider"></li>
                            <li><a href="script/logoutScript.php"><i class="fa fa-sign-out fa-fw"></i> Đăng xuất</a>
                            </li>
                        </ul>
                        <!-- /.dropdown-user -->
                    </li>
                    <!-- /.dropdown -->
                </ul>
                <!-- /.navbar-top-links -->

                <div class="navbar-default sidebar" role="navigation">
                    <div class="sidebar-nav navbar-collapse">
                        <ul class="nav" id="side-menu">
                            <?php
                            if ($_SESSION['role'] == 1) {
                                ?>
                                <li>
                                    <a class="active" href="statistic.php"><i class="fa fa-bar-chart-o fa"></i> &nbspThống kê<span class="fa arrow"></span></a>
                                    <ul class="nav nav-second-level">
                                        <li>
                                            <a href="statistic.php">Doanh thu</a>
                                        </li>
                                    </ul>
                                </li>

                                <li>
                                    <a href="users.php"><i class="fa fa-user fa-fw"></i> Quản lí nhân viên</a>

                                    <!-- /.nav-second-level -->
                                </li>
                                <?php
                            }
                            ?>
                            <li>
                                <a href="customers.php"><i class="fa fa-wrench fa-fw"></i>&nbsp;Quản lí khách hàng</a>

                                <!-- /.nav-second-level -->
                            </li>
                            <li>
                                <a href="#"><i class="fa fa-shopping-cart fa-fw"></i> Quản lí sản phẩm<span class="fa arrow"></span></a>
                                <ul class="nav nav-second-level">
                                    <li>
                                        <a href="categories.php">Danh mục</a>
                                    </li>
                                    <li>
                                        <a href="manufacturers.php">Nhà sản xuất</a>
                                    </li>                                    
                                    <li>
                                        <a href="products.php">Sản phẩm</a>
                                    </li>
                                </ul>
                            </li>
                            <li>

                                <a href="#"><i class="fa fa-edit fa-fw"></i> Quản lí đơn hàng<span class="fa arrow"></span></a>
                                <ul class="nav nav-second-level">
                                    <li>
                                        <a href="orders_in.php">Hóa đơn nhập</a>
                                    </li>
                                    <li>
                                        <a href="orders_out.php">Hóa đơn xuất</a>
                                    </li>

                                </ul>
                            </li>
                        </ul>
                        <!-- /.nav-second-level -->
                    </div>
                    <!-- /.sidebar-collapse -->
                </div>
                <!-- /.navbar-static-side -->
            </nav>

            <div id="page-wrapper">
                <div class="row">
                    <div class="col-lg-12">
                        <h1 class="panel-heading"><p class="text-danger">THỐNG KÊ<p></h1><hr>
                    </div>
                    <!-- /.col-lg-12 -->
                </div>
                <!-- /.row -->
                <div class="row">
                    <div class="col-lg-3 col-md-6">
                        <div class="panel panel-info">
                            <div class="panel-heading">
                                <div class="row">
                                    <div class="col-xs-3" style="padding-top: 25px">
                                        <i class="fa fa-comments fa-5x"></i>
                                    </div>
                                    <div class="col-xs-9 text-right">
                                        <div class="big"><?php countCategories() ?></div>
                                        <div>Danh mục</div>
                                        <div class="big"><?php countManufacturers() ?></div>
                                        <div>Nhà sản xuất</div>
                                        <div class="big"><?php countProducts() ?></div>
                                        <div>Sản phẩm</div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="col-lg-3 col-md-8">
                        <div class="panel panel-info">
                            <div class="panel-heading">
                                <div class="row">
                                    <div class="col-xs-3" style="padding-top: 25px">
                                        <i class="fa fa-user fa-5x"></i>
                                    </div>
                                    <div class="col-xs-9 text-right">
                                        <div class="big">&nbsp;</div>
                                        <div>&nbsp;</div>
                                        <div class="big"><?php countCustomers() ?></div>
                                        <div>Khách hàng</div>
                                        <div class="big">&nbsp;</div>
                                        <div>&nbsp;</div>
                                    </div>
                                </div>
                            </div>

                        </div>
                    </div>
                    <div class="col-lg-3 col-md-6">
                        <div class="panel panel-info">
                            <div class="panel-heading">
                                <div class="row">
                                    <div class="col-xs-3" style="padding-top: 25px">
                                        <i class="fa fa-shopping-cart fa-5x"></i>
                                    </div>
                                    <div class="col-xs-9 text-right">
                                        <div class="big"><?php countOrdersOut() ?></div>
                                        <div>Hóa đơn bán</div>
                                        <div class="big"><?php countOrdersIn() ?></div>
                                        <div>Hóa đơn nhập</div>
                                        <div class="big">&nbsp;</div>
                                        <div>&nbsp;</div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="col-lg-3 col-md-6">
                        <div class="panel panel-info">
                            <div class="panel-heading">
                                <div class="row">
                                    <div class="col-xs-3" style="padding-top: 25px">
                                        <i class="fa fa-support fa-5x"></i>
                                    </div>
                                    <div class="col-xs-9 text-right">
                                        <div class="big">&nbsp;</div>
                                        <div>&nbsp;</div>
                                        <div class="big"><?php countIncome() ?></div>
                                        <div>Doanh thu</div>
                                        <div class="big">&nbsp;</div>
                                        <div>&nbsp;</div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <!-- /.row -->

                <div class="row">
                    <form method="post">
                        Bắt đầu : <input type="date" name="from">
                        Kết thúc : <input type="date" name="to">
                        <br>
                        <br>
                        <div class="form-group">
                            <label>Thống kê theo: </label>
                            <label class="checkbox-inline">
                                <input type="checkbox" name="sort[]" value="day" checked="true">Ngày
                            </label>
                            <label class="checkbox-inline">
                                <input type="checkbox" name="sort[]" value="month">Tháng
                            </label>
                            <label class="checkbox-inline">
                                <input type="checkbox" name="sort[]" value="year">Năm
                            </label>
                        </div>
                        <input type="submit" value="Thống kê" name="submit">
                    </form>
                </div>

                <div class="row">
                    <?php
                    if (isset($_POST['submit'])) {
                        //$sql = "SELECT day(`orders`.`time`) as `day`, month(`orders`.`time`) as `month`, year(`orders`.`time`) as `year`, TRUNCATE(`orders_details`.`quantity` * `products`.`price` * (1 - `orders_details`.`sale_off`/100), 0) as `revenue` FROM `orders`, `orders_details`, `products` WHERE `orders`.`id` = `orders_details`.`order` AND `orders_details`.`product` = `products`.`id` AND date(`orders`.`time`) >= '{$_POST['from']}' AND date(`orders`.`time`) <= '{$_POST['to']}' GROUP BY day(`orders`.`time`), month(`orders`.`time`), year(`orders`.`time`)";
                        $select_day = "";
                        $select_month = "";
                        $select_year = "";
                        $groupby_day = "";
                        $groupby_month = "";
                        $groupby_year = "";
                        for ($i = 0; $i < count($_POST['sort']); $i++) {
                            if ($_POST['sort'][$i] == "day") {
                                $select_day = " day(`orders`.`time`) as `day`,";
                                $groupby_day = " day(`orders`.`time`),";
                            } elseif ($_POST['sort'][$i] == "month") {
                                $select_month = " month(`orders`.`time`) as `month`,";
                                $groupby_month = " month(`orders`.`time`),";
                            } else {
                                $select_year = " year(`orders`.`time`) as `year`,";
                                $groupby_year = " year(`orders`.`time`),";
                            }
                            $from = $_POST['from'];
                            $arr=explode("/", $from);
                            $from = $arr[2]."/".$arr[1]."/".$arr[0];
                            $to = $_POST['to'];
                            $arr=explode("/", $to);
                            $to = $arr[2]."/".$arr[1]."/".$arr[0];
                        }
                        $sql = "SELECT {$select_day}{$select_month}{$select_year} TRUNCATE(SUM(`orders_details`.`quantity` * `products`.`price` * (1 - `orders_details`.`sale_off`/100)), 0) as `revenue` FROM `orders`, `orders_details`, `products` "
                                . "WHERE `orders`.`id` = `orders_details`.`order` AND `orders_details`.`product` = `products`.`id` AND date(`orders`.`time`) >= '{$from}' AND date(`orders`.`time`) <= '{$to}' "
                                . "GROUP BY {$groupby_day}{$groupby_month}{$groupby_year}";
                        $sql = substr($sql, 0, strlen($sql) - 1);

                        $a = "<div class='row'>
                                <div class='col-lg-12'>
                                    <div class='panel panel-default'>
                                        <div class='panel-heading'>
                                            Các danh mục
                                        </div> 
                                        <div class='panel-body'>
                                            <div class='table-responsive'>
                                                <table class='table table-striped table-bordered table-hover' id='dataTables-example'>
                                                    <thead>
                                                        <tr>
                                                            <th>Thống kê theo:</th>
                                                            <th>Doanh thu:</th>                                            
                                                        </tr>
                                                    </thead>
                                                    <tbody>";
                        $result = mysqli_query(getConnect(), $sql);
                        while ($row = mysqli_fetch_array($result)) {
                            $a = $a . "     <tr class='odd gradeX'>"
                                    . "         <td>";
                            for ($i = 0; $i < count($_POST['sort']); $i++) {
                                if ($_POST['sort'][$i] == "day") {
                                    $a = $a . "Ngày ${row['day']}";
                                } else if ($_POST['sort'][$i] == "month") {
                                    $a = $a . " Tháng ${row['month']}";
                                } else if ($_POST['sort'][$i] == "year") {
                                    $a = $a . " Năm ${row['year']}";
                                }
                            }
                            $a = $a . "</td>";
                            $a = $a . "
                                                            <td>${row['revenue']}</td>
                                                        </tr>";
                        }
                        $a = $a . "   
                                                    </tbody>
                                                </table>
                                            </div>
                                        </div>
                                    </div>
                                </div> 
                            </div>";
                        echo $a;
                    }
                    ?>
                </div>
                <!-- /.row -->
            </div>
            <!-- /#page-wrapper -->

        </div>
        <!-- /#wrapper -->

        <!-- jQuery Version 1.11.0 -->
        <script src="js/jquery-1.11.0.js"></script>

        <!-- Bootstrap Core JavaScript -->
        <script src="js/bootstrap.min.js"></script>

        <!-- Metis Menu Plugin JavaScript -->
        <script src="js/plugins/metisMenu/metisMenu.min.js"></script>

        <!-- Morris Charts JavaScript -->
        <script src="js/plugins/morris/raphael.min.js"></script>
        <script src="js/plugins/morris/morris.min.js"></script>
        <script src="js/plugins/morris/morris-data.js"></script>

        <!-- Custom Theme JavaScript -->
        <script src="js/sb-admin-2.js"></script>

        <!-- DataTables JavaScript -->
        <script src="js/plugins/dataTables/jquery.dataTables.js"></script>
        <script src="js/plugins/dataTables/dataTables.bootstrap.js"></script>

        <script>
            $(document).ready(function() {
                $('#dataTables-example').dataTable();

            });
        </script>
    </body>

</html>
